from django.db import models
from django.utils.timezone import now
from django.urls import reverse_lazy
from users.models import CCUser

def artist_path(instance, filename):
    return 'music/{0}/{1}'.format(instance.artist.handle, filename)

#def album_path(instance, filename):
#    return 'music/{0}/{1}/{2}'.format(instance.album.artist.handle, instance.album.title, filename)

def album_path(instance, filename):
    return 'music/{0}/{1}/{2}'.format(instance.artist.handle, instance.title, filename)

def song_path(instance, filename):
    return 'music/{0}/{1}/{2}'.format(instance.artist.handle, instance.album.title, filename)


class Artist(models.Model):
    handle = models.SlugField(max_length=50, primary_key=True)
    name = models.CharField(max_length=200)
    bio = models.CharField(max_length=1000, null=True, blank=True)
    admins = models.ManyToManyField(CCUser,
                                    through='ArtistAdmin',
                                    through_fields=('artist', 'admin'),
                                    related_name='admin',
                                   )
    members = models.ManyToManyField(CCUser,
                                     through='BandMember',
                                     through_fields=('artist', 'member'),
                                     related_name='member',
                                    )
    logo = models.ImageField(upload_to=artist_path, null=True, blank=True)
    created_date = models.DateField(auto_now_add=True)
    modified_date = models.DateField(auto_now=True)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse_lazy('artist', kwargs={'artist_handle': self.handle})


class Role(models.Model):
    role = models.CharField(max_length=30, primary_key=True)

    def __str__(self):
        return self.role


class ArtistAdmin(models.Model):
    admin = models.ForeignKey(CCUser, on_delete=models.CASCADE)
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)


class BandMember(models.Model):
    member = models.ForeignKey(CCUser, on_delete=models.CASCADE)
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
    #role = models.ForeignKey(Role, on_delete=models.CASCADE)


class License(models.Model):
    abbreviation = models.CharField(max_length=20, primary_key=True)
    name = models.CharField(max_length=100)
    version = models.DecimalField(max_digits=3, decimal_places = 1)
    large_image = models.URLField()
    small_image = models.URLField()
    legaleze = models.URLField()

    def __str__(self):
        return self.abbreviation


class Album(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200)
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
    cover = models.ImageField(upload_to=album_path, null=True, blank=True)
    license = models.ForeignKey(License, on_delete=models.SET_NULL, null=True)
    release_date = models.DateField(default=now())
    created_date = models.DateField(auto_now_add=True)
    modified_date = models.DateField(auto_now=True)

    def __str__(self):
        return self.artist.name + ' - ' + self.title


class Song(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200)
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
    album = models.ForeignKey(Album, on_delete=models.CASCADE)
    license = models.ForeignKey(License, on_delete=models.SET_NULL, null=True)
    nsfw = models.BooleanField(default=False)
    track = models.IntegerField(null=True)
    duration = models.CharField(max_length=10, null=True)
    file = models.FileField(upload_to=song_path, max_length=200)
    release_date = models.DateField(default=now())
    created_date = models.DateField(auto_now_add=True)
    modified_date = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.artist.name + ' - ' + self.title + ' (' + self.album.title + ')'


class Queue(models.Model):
    song = models.ForeignKey(Song, on_delete=models.CASCADE)
    requester = models.ForeignKey(CCUser, on_delete=models.CASCADE)


class Rating(models.Model):
    song = models.ForeignKey(Song, on_delete=models.CASCADE)
    user = models.ForeignKey(CCUser, on_delete=models.CASCADE)
    rating = models.IntegerField()
